﻿using Models.Mailbox;
using Models.Mailbox.Mail;
using Newtonsoft.Json;
using SendMessagePage.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SendMessagePage.HomePage
{
    /// <summary>
    /// UpdateTaskPage.xaml 的交互逻辑
    /// </summary>
    public partial class UpdateTaskPage : Window
    {
        TaskClass taskClass = new TaskClass();

        //修改定时任务
        private string UpdateMailboxConfigureUrl = ConfigurationManager.AppSettings["UpdateMailboxConfigure"];

        public UpdateTaskPage(TaskClass task):this()
        {
            taskClass = task;
            this.txtTaskName.Text = taskClass.TaskName;
            this.txtTask.Text = taskClass.TaskRemark;
            this.dpTaskTime.Text = taskClass.TaskTime;
            if (task.TaskState.Equals("开启"))
            {
                this.btn_Open.IsChecked = true;
            }
            else {
                this.btn_Close.IsChecked = true;
            }

        }
        public UpdateTaskPage()
        {
            InitializeComponent();
        }

        private async void btn_Update(object sender, RoutedEventArgs e)
        {
            if (txtTaskName.Text.Length == 0)
            {
                MessageBox.Show("热为奴名称不能为空", "登录提示");
                this.txtTaskName.Focus();
                return;
            }

            if (txtTask.Text.Length == 0)
            {
                MessageBox.Show("任务描述不能为空", "登录提示");
                this.txtTask.Focus();
                return;
            }

            if (dpTaskTime.Text.Length == 0)
            {
                MessageBox.Show("执行时间不能为空", "登录提示");
                this.dpTaskTime.Focus();
                return;
            }


            MailboxTaskConfig config =new MailboxTaskConfig();
            config.Id = taskClass.Id;
            config.TaskName=taskClass.TaskName;
            config.TaskRemark=taskClass.TaskRemark;
            config.TaskTime = Convert.ToDateTime(taskClass.TaskTime);
            config.TaskState = (bool)btn_Open.IsChecked ? 0 : 2;


            string json = JsonConvert.SerializeObject(config);
            string result = await AccessWebApiUtil.PostAsyncJson(UpdateMailboxConfigureUrl, json, PackingUserLogin.token);

            OrderResult order = JsonConvert.DeserializeObject<OrderResult>(result);

            if (order.code == 200)
            {
                MessageBox.Show("修改成功!");

            }
            else
            {
                MessageBox.Show("修改成功!");

            }

            this.Close();
        }
    }
}
